package com.example.demo_text;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/UpdateAllEnrollmentsServlet")
public class UpdateAllEnrollmentsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        String[] selectedCourseIds = request.getParameterValues("courseId");

        // 删除该用户原有选课记录
        EnrollmentDAO.deleteAllEnrollmentsByUserId(userId);

        // 插入新选课记录
        if (selectedCourseIds != null) {
            for (String courseIdStr : selectedCourseIds) {
                int courseId = Integer.parseInt(courseIdStr);
                EnrollmentDAO.addEnrollment(userId, courseId);
            }
        }

        response.sendRedirect("manageCourse.jsp");
    }
}
